package com.atguigu.springcloud.alibaba.controller;

import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author zengbang
 * @since 2020/12/30 15:20
 */
@Slf4j
@RestController
@RequestMapping(value = "demo")
public class DemoController {
    private static int second = 0;

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    RestTemplate restTemplate1;

    @GetMapping(value = "/testip")
    public String testip(String ip) {
        HttpEntity entity = new HttpEntity<>(null, null);
        try {
            ResponseEntity<String> exchange = restTemplate.exchange("http://" + ip + ":8401/getpodinfo", HttpMethod.GET, entity, String.class);
            System.out.println("有人让我给" + ip + "ip发消息，我发了！");
            return exchange.getBody();
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @GetMapping(value = "/testservicename")
    public String testservicename(String servicename) {
        HttpEntity entity = new HttpEntity<>(null, null);
        try {
            ResponseEntity<String> exchange = restTemplate1.exchange("http://" + servicename + ":8401/getpodinfo", HttpMethod.GET, entity, String.class);
            System.out.println("有人让我给" + servicename + "服务发消息，我发了！");
            return exchange.getBody();
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @GetMapping(value = "/uuid")
    public String getUuid() {
        int sec = new Random().nextInt(10);
        if (sec > second) {

            second = sec;
        }
        try {
            Thread.sleep(second * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return UUID.randomUUID().toString();
    }

    @GetMapping(value = "/max")
    public String getMax() {
        int sec = second;
        second = 0;
        return sec + "";
    }
}
